var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    debug = require('debug')('react-demo'),
    request = require('request')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);

// 系统创建维修单
router.post("/sysCreate", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/sysCreate',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

//添加维修单行
router.post("/addOrderLine", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/addOrderLine',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 添加维修人员信息, 状态改为处理中
router.post("/generate", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/generate',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


// 确认已完成
router.post("/updateComplete", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/updateComplete',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 查询工单
router.post("/query", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/query',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 查询工单行
router.post("/queryLines", function(req, res, next){
  debug("queryLines")
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/queryLines',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  debug(option)
  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 删除工单行
router.post("/deleteLine", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/deleteLine',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 删除工单
router.post("/delete", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/delete',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/cancel", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/cancel',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/pushCompleteToJde", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/pushCompleteToJde',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

// 查询未处理维修数量
router.post("/queryApplingCount", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/queryApplingCount',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


// 线下支付
router.post("/pay_offline", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workOrder/pay_offline',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


// 查询维修账单
router.post("/queryBill", function(req, res, next){
  var token = req.cookies.access_token
  var param = req.body;
  param.appId = req.cookies.appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workBill/queryByTime',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

//导出csv
router.post("/export", function(req, res, next){
  var token = req.cookies.access_token
  var appId = req.cookies.appId;
  var param = req.body;
  param.appId = appId;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/workBill/export',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  console.log(param)
  request(option)
  .on('error', (err) => {
    console.error(err);
    debug(err)
    return res.status(err.statusCode).json({
      success: false,
      errCode: err.statusCode,
      errMsg: err.error?err.error.errMsg:err
    })
  })
  .pipe(res)
})


router.get("/print", function(req, res, next){
  var token = req.cookies.access_token
  var appId = req.cookies.appId;
  var option = {
    method: 'GET',
    uri: config.apiHost+'/api/orderPrint/print?id='+req.query.id,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  request(option)
  .on('error', (err) => {
    console.error(err);
    debug(err)
    return res.status(err.statusCode).json({
      success: false,
      errCode: err.statusCode,
      errMsg: err.error?err.error.errMsg:err
    })
  })
  .pipe(res)
})

module.exports = router
